package org.netEase.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.commentDao;
import org.netEase.dao.impl.commentDaoImple;
import org.netEase.entity.comment;
import org.netEase.entity.shangping;
import org.netEase.entity.yonghu;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
@WebServlet(urlPatterns= {"/frontDeskManager/GoodsCommentAdd.shtml"})
public class GoodsCommentAdd extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SmartUpload smartUpload = new SmartUpload();
		smartUpload.initialize(this, request, response);
		try {
			smartUpload.upload();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}

		Request req = smartUpload.getRequest();
		request.setCharacterEncoding("utf-8");
		String sid = req.getParameter("sid");
		String hid = req.getParameter("hid");
		String goodsname = req.getParameter("goodsname");
		String content = req.getParameter("content");
		String com_praise = req.getParameter("com_praise");
		/*String simg = request.getParameter("img");*/
		Files files = smartUpload.getFiles();
		File file = files.getFile(0);
		comment comment = new comment();
		yonghu yonghu = new yonghu();
		yonghu.setHid(Integer.parseInt(hid));
		comment.setYonghu(yonghu);
		comment.setCom_praise(Integer.parseInt(com_praise));
		shangping shangping =new shangping();
		shangping.setSid(Integer.parseInt(sid));
		comment.setShangping(shangping);
		comment.setCom_content(content);
		
		/*comment.setCom_images(simg);*/
		if(file.isMissing() == false){
			try {
				file.saveAs("./images/index/"+file.getFileName());
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
			comment.setCom_images(file.getFileName());
		}
		commentDao commentDao = new commentDaoImple();
		int rows=commentDao.addcomm(comment);
		if (rows > 0) {
			//response.sendRedirect(request.getContextPath() + "/frontDeskManager/frontDeskComment.shtml?sid="+sid+"&goodsname="+goodsname);
			request.getRequestDispatcher("frontDeskComment.shtml?sid="+sid+"&goodsname="+goodsname).forward(request, response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
